package com.meila.cms.controller;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meila.cms.auth.AuthClass;
import com.meila.cms.entity.AjaxObj;
import com.meila.cms.entity.MypartsLib;
import com.meila.cms.entity.Parts;
import com.meila.cms.model.MyPartsModel;
import com.meila.cms.model.PartDesignModel;
import com.meila.cms.model.PartsDesignModel;
import com.meila.cms.services.IPartDesignService;
import com.meila.cms.util.json.JsonUtil2;
import com.meila.cms.util.page.Pager;

@Controller
@RequestMapping("/front/partDesign")
@AuthClass("login")
public class PartDesignController extends BaseController {

	private IPartDesignService partDesignService;

	public IPartDesignService getPartDesignService() {
		return partDesignService;
	}

	@Inject
	public void setPartDesignService(IPartDesignService partDesignService) {
		this.partDesignService = partDesignService;
	}
	
	/**
	 * 更新字段
	 * @param userModel
	 * @return
	 */
	@RequestMapping("/updatePartDesign")
	@ResponseBody
	public AjaxObj updatePartDesign(PartsDesignModel partsDesignModel) {
		partsDesignModel.setUpdateTime(new Date());
		AjaxObj ajaxObj=partDesignService.updatePartDesign(partsDesignModel);
		
		return ajaxObj;
		  
	}
	
	/**
	 * 创建零件设计
	 * @param partsDesignModel
	 * @return
	 */
	@RequestMapping("/addMyPart")
	@ResponseBody
	public AjaxObj addMyPart(PartsDesignModel partsDesignModel){
		partsDesignModel.setUpdateTime(new Date());
		AjaxObj ajaxObj=partDesignService.addMyPart(partsDesignModel);
		
		return ajaxObj;
	}
	
	/**
	 * 更新字段（我的零部件库）
	 * @param userModel
	 * @return
	 */
	@RequestMapping("/updateMyPartDesign")
	@ResponseBody
	public AjaxObj updateMyPartDesign(MyPartsModel myPartsModel) {
		myPartsModel.setUpdateTime(new Date());
		AjaxObj ajaxObj=partDesignService.updateMyPartDesign(myPartsModel);
		
		return ajaxObj;
		  
	}
	
	/**
	 * 零件新增
	 * @param myPartsModel
	 * @return
	 */
	@RequestMapping("/addPartDesign")
	@ResponseBody
	public AjaxObj addPartDesign(MyPartsModel myPartsModel) {
	 		
		AjaxObj ajaxObj=partDesignService.addPartDesign(myPartsModel);
		
		return ajaxObj;
		  
	}

	/**
	 * 获取零件分页列表集合信息
	 * 
	 * @param model
	 * @return 获取零件分页列表集合信息
	 */
	@RequestMapping(value = "/partDesign_list", method = RequestMethod.GET)
	public String listMyParts(PartDesignModel partDesignModel, Model model) {

		// 获取我的零件分页列表集合信息
		Pager<PartDesignModel> listMyParts = partDesignService
				.findMyParts(partDesignModel);
		// 保存我的零件分页列表集合信息
		model.addAttribute("datas", listMyParts);

		model.addAttribute("jsondatas",
				JsonUtil2.toJSONList(listMyParts.getDatas()));

		return "partDesign/partDesign_list";
	}

	@RequestMapping(value = "/carDesign_list", method = RequestMethod.POST)
	@ResponseBody
	public List listMyParts(MyPartsModel myPartsModel) {

		// 获取我的零件分页列表集合信息
		// Pager<MyPartsModel> listMyParts =
		// partDesignService.findMyParts(myPartsModel);

		return null;
	}

	@RequestMapping("/newDesign")
	public String newDesign(Model model) {

		return "partDesign/newDesign";
	}

	/**
	 * 跳转到修改界面
	 * 
	 * @param partsDesignModel
	 * @param model
	 * @return
	 */
	@RequestMapping("/partDesignModify")
	public String partDesignModify(PartsDesignModel partsDesignModel,Model model) {

		partDesignService.partDesignToAddEdit(partsDesignModel,model);

		return "partDesign/partDesignModify";
	}
	
	/**
	 * 跳转到新增界面
	 * 
	 * @param myPartsModel
	 * @param model
	 * @return
	 */
	@RequestMapping("/myPartDesignModify")
	public String partDesignAdd(MyPartsModel myPartsModel,Model model){
		
		partDesignService.partDesignToAdd(myPartsModel, model);
		
		return "partDesign/myPartDesignModify";
	}

	/**
	 * 根据零件类型获取零件列表
	 * 
	 * @param partsDesignModel
	 * @return
	 */
	@RequestMapping("/getPartList")
	@ResponseBody
	public List getPartList(PartsDesignModel partsDesignModel) {

		List list = partDesignService.getPartList(partsDesignModel);

		return list;
	}
	
	/**
	 * 根据零件类型获取我的零件列表
	 * 
	 * @param partsDesignModel
	 * @return
	 */
	@RequestMapping("/getMyPartList")
	@ResponseBody
	public List getMyPartList(MyPartsModel myPartsModel) {

		List list = partDesignService.getMyPartList(myPartsModel);

		return list;
	}

	/**
	 * 根据零件类型获取零件列表
	 * 
	 * @param partsPropertyModel
	 * @return
	 */
	@RequestMapping("/getPropertyList")
	@ResponseBody
	public List getPropertyList(PartsDesignModel partsDesignModel) {

		List list = partDesignService.getPropertyList(partsDesignModel);

		return list;
	}
	
	/**
	 * 根据零件类型获取我的零件列表
	 * 
	 * @param myPartsPropertyModel
	 * @return
	 */
	@RequestMapping("/getMyPropertyList")
	@ResponseBody
	public List getMyPropertyList(MyPartsModel myPartsModel){
		
		List list = partDesignService.getMyPropertyList(myPartsModel);
		
		return list;
	}	
	
	//选择性下载文件（我的零件库下载）
	@RequestMapping("/downloadFileBychose")
	public 	  ResponseEntity<byte[]>  downloadFileBychose(String designid,Integer[] chose) {
			
		ResponseEntity<byte[]> result = null;
		 
		result = partDesignService.downloadFileBychose(designid,chose,request,response);
	 
	    return result;  
			
			
	}
	
	//选择性下载文件（零件库下载）
	@RequestMapping("/downloadFileBychoseFromParts")
	public ResponseEntity<byte[]> downloadFileBychoseFromParts(String designid,Integer[] chose){
		
		ResponseEntity<byte[]> result = null;
		
		result = partDesignService.dowloadFileBychoseFromParts(designid,chose,request,response);
		
		return result;
		
	}
	
	//上传相关图纸
	@RequestMapping("/update")
	public String update(MypartsLib mypartsLib){
		
		partDesignService.update(mypartsLib);
		
		return "redirect:/front/partDesign/partDesign_list";
	}
	
	//删除零件设计
	@RequestMapping("/delPartDesign")
	@ResponseBody
	public AjaxObj delPartDesign(PartDesignModel partDesignModel){
		
		AjaxObj ajaxObj=partDesignService.delPartDesign(partDesignModel);
		
		return ajaxObj;
	}
	
	//判断零件设计是否已添加我的零件
	@RequestMapping("/whetherToAdd")
	@ResponseBody
	public AjaxObj whetherToAdd(PartDesignModel partDesignModel){
		
		AjaxObj ajaxObj=partDesignService.whetherToAdd(partDesignModel);
		
		return ajaxObj;
	}
	
	//设计名是否存在验证(t_parts_design)
	@RequestMapping("/designameVerify")
	@ResponseBody
	public AjaxObj designameVerify(PartDesignModel partDesignModel){
		
		AjaxObj ajaxObj=partDesignService.designameVerify(partDesignModel);
		
		return ajaxObj;
	}
	
	//新建设计时验证设计名是否存在
	@RequestMapping("/designameVerifyInNewDesign")
	@ResponseBody
	public AjaxObj designameVerifyInNewDesign(PartDesignModel partDesignModel){
		
		AjaxObj ajaxObj=partDesignService.designameVerifyInNewDesign(partDesignModel);
		
		return ajaxObj;
	}
	
	/**
	 * 根据零件id检查剩余空间是否够上传
	 * @param fileids
	 * @param isupdate
	 * @return
	 */
	@RequestMapping("/checkMemory")
	@ResponseBody
	public AjaxObj checkMemory(Integer id,Boolean isupdate){
		
		AjaxObj ajaxObj;
		Parts parts=partsMapper.selectByPrimaryKey(id);
		
		Integer[] fileids={parts.getTwoDid(),parts.getThreeDid(),parts.getProgrammeid(),parts.getProductid(),parts.getModleid()};
		
		ajaxObj=partDesignService.updateMemory(fileids,isupdate);
		
		return ajaxObj;
	}

}